/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.naga.bulksender.main;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.naga.bulksender.callback.InboundMessageCallback;
import org.naga.bulksender.data.BulkMessage;
import org.naga.bulksender.service.BulkServiceIfc;
import org.naga.bulksender.service.util.ServiceLocator;
import org.smslib.InboundMessage;
import org.smslib.Message;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 *
 * @author Prueba
 */
public class BulkSenderMain implements Runnable {

    private Log log = LogFactory.getLog(BulkSenderMain.class);
    private boolean running = false;
    //private static AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("conf/applicationContext.xml");

    public void run() {
        try {
            log.info("Running the BulkSenderMain");
            BulkServiceIfc service = (BulkServiceIfc) ServiceLocator.getInstance().getObject("bulkService");
            service.startService();
            running = true;
            while (running) {
                List<BulkMessage> lstMsg = service.checkNewMsgToSend();
                if (null != lstMsg && lstMsg.size() > 0) {
                    service.scheduleNewMessages(lstMsg);
                }
                try {
                    Thread.sleep(10000);
                } catch (Exception ex) {
                    log.error("Exception in the Sleep=>", ex);
                }
            }
            service.stopService();
            log.info("Leave the Run BulkSenderMain");
        } catch (Exception exc) {
            log.error("Exception in the Run at BulkSenderMain", exc);
        }
    }

    public void begin() {
        Thread th = new Thread(this);
        th.start();
    }

    public void finish() {
        running = false;
    }

    public static void main(String[] args) {
        try {
            AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("conf/applicationContext.xml");

            // add a shutdown hook for the above context...
            // ctx.registerShutdownHook();

            /*    HelloWorld hw = (HelloWorld)ctx.getBean("hello");
            System.out.println("Value =>" + hw.getString());

            BulkMessageDaoIfc msgDao = (BulkMessageDaoIfc)ctx.getBean("messageDao");
            msgDao.checkNewMessages();

            List<BulkMessage> newMsgLst = msgDao.getNewMessages();
            for ( BulkMessage msg: newMsgLst ){
            msgDao.getNumbersFromContacts(msg);
            msgDao.getNumbersFromGroup(msg);
            System.out.println("===================================");
            OutboundMessage outMsg = (OutboundMessage)msg;
            System.out.println("New Msg=>" + outMsg.getText());
            //msgDao.changeMessageStatus(msg.getBulkSmsId(),"Scheduled");
            for ( String numbers: msg.getLstNumberFromContacts()){
            System.out.println("Number COntacts=>" + numbers );
            }
            for ( String numbers: msg.getLstNumberFromGroups()){
            System.out.println("Number Grupos=>" + numbers );
            }
            }


            //  List<String> lstNUmbers = msgDao.getNumbersFromContacts(3);
            //  System.out.println("List New MSG =>" + lstNUmbers);

            //  DeviceConfiguration config = (DeviceConfiguration)ctx.getBean("deviceConfig");
            //System.out.println("Value =>" +config.getDevices().size());
             */
           // BulkServiceIfc service = (BulkServiceIfc) ctx.getBean("bulkService");
            //BulkMessageDaoIfc dao = (BulkMessageDaoIfc)ctx.getBean("messageDao");
            //dao.logMsgSent(120,"987654321");
            //service.startService();
            InboundMessageCallback inCall = (InboundMessageCallback)ctx.getBean("inCallback");
            InboundMessage iMsg = new InboundMessage( Message.MessageTypes.INBOUND,9,"Hello World thisa Test"); 
            iMsg.addText("Pollito CHiken");            
            inCall.process("com3.port",Message.MessageTypes.INBOUND,iMsg);
            System.out.println("Value =>" + inCall);
        } catch (Exception e) {
            System.out.println("Exception in the Main" + e);
        }

    }
}
